<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>worker.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_gears_worker.js.html">worker.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This represents a Gears worker (background process).
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.gears.Worker&#39;);
<a name="line21"></a>goog.provide(&#39;goog.gears.Worker.EventType&#39;);
<a name="line22"></a>goog.provide(&#39;goog.gears.WorkerEvent&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line25"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * This is an absctraction of workers that can be used with Gears WorkerPool.
<a name="line31"></a> * @constructor
<a name="line32"></a> * @param {goog.gears.WorkerPool} workerPool  WorkerPool object.
<a name="line33"></a> * @param {number} opt_id  The id of the worker this represents.
<a name="line34"></a> *
<a name="line35"></a> * @extends {goog.events.EventTarget}
<a name="line36"></a> */
<a name="line37"></a>goog.gears.Worker = function(workerPool, opt_id) {
<a name="line38"></a>  goog.events.EventTarget.call(this);
<a name="line39"></a>
<a name="line40"></a>  /**
<a name="line41"></a>   * Reference to the worker pool.
<a name="line42"></a>   * @type {goog.gears.WorkerPool}
<a name="line43"></a>   * @private
<a name="line44"></a>   */
<a name="line45"></a>  this.workerPool_ = workerPool;
<a name="line46"></a>
<a name="line47"></a>  if (opt_id != null) {
<a name="line48"></a>    this.init(opt_id);
<a name="line49"></a>  }
<a name="line50"></a>};
<a name="line51"></a>goog.inherits(goog.gears.Worker, goog.events.EventTarget);
<a name="line52"></a>
<a name="line53"></a>
<a name="line54"></a>/**
<a name="line55"></a> * Called when we receive a message from this worker. The message will
<a name="line56"></a> * first be dispatched as a WorkerEvent with type {@code EventType.MESSAGE} and
<a name="line57"></a> * then a {@code EventType.COMMAND}. An EventTarget may call
<a name="line58"></a> * {@code WorkerEvent.preventDefault()} to stop further dispatches.
<a name="line59"></a> * @param {GearsMessageObject} messageObject An object containing all
<a name="line60"></a> *     information about the message.
<a name="line61"></a> */
<a name="line62"></a>goog.gears.Worker.prototype.handleMessage = function(messageObject) {
<a name="line63"></a>  // First dispatch a message event.
<a name="line64"></a>  var messageEvent = new goog.gears.WorkerEvent(
<a name="line65"></a>      goog.gears.Worker.EventType.MESSAGE,
<a name="line66"></a>      messageObject);
<a name="line67"></a>
<a name="line68"></a>  // Allow the user to call prevent default to not process the COMMAND.
<a name="line69"></a>  if (this.dispatchEvent(messageEvent)) {
<a name="line70"></a>    if (goog.gears.Worker.isCommandLike(messageObject.body)) {
<a name="line71"></a>      this.dispatchEvent(new goog.gears.WorkerEvent(
<a name="line72"></a>          goog.gears.Worker.EventType.COMMAND,
<a name="line73"></a>          messageObject));
<a name="line74"></a>    }
<a name="line75"></a>  }
<a name="line76"></a>};
<a name="line77"></a>
<a name="line78"></a>
<a name="line79"></a>/**
<a name="line80"></a> * The ID of the worker we are communicating with.
<a name="line81"></a> * @type {number?}
<a name="line82"></a> * @private
<a name="line83"></a> */
<a name="line84"></a>goog.gears.Worker.prototype.id_ = null;
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * Initializes the worker object with a worker id.
<a name="line89"></a> * @param {number} id  The id of the worker this represents.
<a name="line90"></a> */
<a name="line91"></a>goog.gears.Worker.prototype.init = function(id) {
<a name="line92"></a>  if (this.id_ != null) {
<a name="line93"></a>    throw Error(&#39;Can only set the worker id once&#39;)
<a name="line94"></a>  }
<a name="line95"></a>
<a name="line96"></a>  this.id_ = id;
<a name="line97"></a>  this.workerPool_.registerWorker(this);
<a name="line98"></a>};
<a name="line99"></a>
<a name="line100"></a>
<a name="line101"></a>/**
<a name="line102"></a> * Sends a command to the worker.
<a name="line103"></a> * @param {number} commandId  The ID of the command to
<a name="line104"></a> *     send.
<a name="line105"></a> * @param {Object} params An object to send as the parameters. This object
<a name="line106"></a> *     must be something that Gears can serialize. This includes JSON as well
<a name="line107"></a> *     as Gears blobs.
<a name="line108"></a> */
<a name="line109"></a>goog.gears.Worker.prototype.sendCommand = function(commandId, params) {
<a name="line110"></a>  this.sendMessage([commandId, params]);
<a name="line111"></a>};
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Sends a message to the worker.
<a name="line116"></a> * @param {*} message The message to send to the target worker.
<a name="line117"></a> */
<a name="line118"></a>goog.gears.Worker.prototype.sendMessage = function(message) {
<a name="line119"></a>  this.workerPool_.sendMessage(message, this);
<a name="line120"></a>};
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Gets an ID that uniquely identifies this worker. The ID is unique among all
<a name="line125"></a> * worker from the same WorkerPool.
<a name="line126"></a> *
<a name="line127"></a> * @return {number} The ID of the worker. This might be null if the
<a name="line128"></a> *     worker has not been initialized yet.
<a name="line129"></a> */
<a name="line130"></a>goog.gears.Worker.prototype.getId = function() {
<a name="line131"></a>  if (this.id_ == null) {
<a name="line132"></a>    throw Error(&#39;The worker has not yet been initialized&#39;);
<a name="line133"></a>  }
<a name="line134"></a>  return this.id_;
<a name="line135"></a>};
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Whether an object looks like a command. A command is an array with length 2
<a name="line140"></a> * where the first element is a number.
<a name="line141"></a> * @param {*} obj The object to test.
<a name="line142"></a> * @return {boolean} true if the object looks like a command.
<a name="line143"></a> */
<a name="line144"></a>goog.gears.Worker.isCommandLike = function(obj) {
<a name="line145"></a>  return goog.isArray(obj) &amp;&amp; obj.length == 2 &amp;&amp;
<a name="line146"></a>      goog.isNumber((/** @type {Array} */ obj)[0]);
<a name="line147"></a>};
<a name="line148"></a>
<a name="line149"></a>
<a name="line150"></a>/**
<a name="line151"></a> * Disposes of the object.
<a name="line152"></a> */
<a name="line153"></a>goog.gears.Worker.prototype.disposeInternal = function() {
<a name="line154"></a>  goog.gears.Worker.superClass_.disposeInternal.call(this);
<a name="line155"></a>  this.workerPool_.unregisterWorker(this);
<a name="line156"></a>  this.workerPool_ = null;
<a name="line157"></a>};
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>
<a name="line161"></a>/**
<a name="line162"></a> * Enum for event types fired by the worker.
<a name="line163"></a> * @enum {string}
<a name="line164"></a> */
<a name="line165"></a>goog.gears.Worker.EventType = {
<a name="line166"></a>  MESSAGE: &#39;message&#39;,
<a name="line167"></a>  COMMAND: &#39;command&#39;
<a name="line168"></a>};
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>
<a name="line172"></a>/**
<a name="line173"></a> * Event used when the worker recieves a message
<a name="line174"></a> * @param {string} type  The type of event.
<a name="line175"></a> * @param {GearsMessageObject} messageObject  The message object.
<a name="line176"></a> *
<a name="line177"></a> * @constructor
<a name="line178"></a> * @extends {goog.events.Event}
<a name="line179"></a> */
<a name="line180"></a>goog.gears.WorkerEvent = function(type, messageObject) {
<a name="line181"></a>  goog.events.Event.call(this, type);
<a name="line182"></a>
<a name="line183"></a>  /**
<a name="line184"></a>   * The message sent from the worker.
<a name="line185"></a>   * @type {*}
<a name="line186"></a>   */
<a name="line187"></a>  this.message = messageObject.body;
<a name="line188"></a>
<a name="line189"></a>  /**
<a name="line190"></a>   * The JSON object sent from the worker.
<a name="line191"></a>   * @type {*}
<a name="line192"></a>   * @deprecated Use message instead.
<a name="line193"></a>   */
<a name="line194"></a>  this.json = this.message;
<a name="line195"></a>
<a name="line196"></a>  /**
<a name="line197"></a>   * The object containing all information about the message.
<a name="line198"></a>   * @type {GearsMessageObject}
<a name="line199"></a>   */
<a name="line200"></a>  this.messageObject = messageObject;
<a name="line201"></a>};
<a name="line202"></a>goog.inherits(goog.gears.WorkerEvent, goog.events.Event);
</pre>


</body>
</html>
